package cn.jhc.sevlet;//个人成绩页面

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.jhc.bean.Score;
import cn.jhc.bean.Student;
import cn.jhc.bean.Study;
import cn.jhc.bean.Teacher;
import cn.jhc.dao.ScoreDao;
import cn.jhc.dao.StudentDao;
import cn.jhc.dao.StudyDao;
import cn.jhc.dao.TeacherDao;
import cn.jhc.dao.impl.ScoreDaoImp;
import cn.jhc.dao.impl.StudentDaoImpl;
import cn.jhc.dao.impl.StudyDaoImpl;
import cn.jhc.dao.impl.TeacherDaoImpl;

/**
 * Servlet implementation class ScoreServlet
 */
@WebServlet("/user/student/score.do")
public class ScoreServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ScoreServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();
		String username = session.getAttribute("username").toString();
		StudentDao studentDao = new StudentDaoImpl();
		Student student = studentDao.getStudent(username);
		Calendar c = Calendar.getInstance();
		c.setTime(student.getEntertime());
		int year = c.get(Calendar.YEAR);
		Set<String> times = new HashSet<String>();
		for(int i=0;i<3;i++){
			String time = (year+i)+"-"+(year+i+1);
			times.add(time);
		}
		int sid = student.getId();
		ScoreDao scoredao = new ScoreDaoImp();
		List<Score> scores = scoredao.list(sid);
		/*StudyDao studyDao = new StudyDaoImpl();
		TeacherDao teacherDao = new TeacherDaoImpl();	
		List<Study> studies = studyDao.list();	
		List<Teacher> teachers = new ArrayList(); 
		
		for(Score score : scores){
			for(Study study : studies){
				if(study.getCourse().getCoursename().equals(score.getCourse().getCoursename())
				&& study.getClazz().getName().equals(score.getStudent().getClazz().getName())){
					Teacher teacher = (Teacher) teacherDao.get(study.getTeacher().getTid());
					teachers.add(teacher);
				}
			}
		}
		request.setAttribute("teachers", teachers);*/
		request.setAttribute("scores", scores);
		request.setAttribute("times", times);
		request.setAttribute("year", year);
		System.out.println(request.getContextPath()+"/user/student/score.jsp");
		request.getRequestDispatcher("score.jsp").forward(request,
				response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
